package com.timu;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

public class User {
    /* 记录当前产生到第几个对象了 */
    private static int current_index = 1;
    private Integer id;
    private String userCode;
    private String name;
    private String phone;
    private LocalDate birthday;
    private Integer age;
    private final Date created = new Date();
    private Date updated;
    /**
     * 会员信息
     */
    private Vip vip;
    /* 时间转换格式 */
    private static final DateFormat DATE_FORMAT =
            new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public User() {
        init();
    }

    public User(String name, String phone, LocalDate birthday) {
        init();
        updateName(name);
        updatePhone(phone);
        updateBirthday(birthday);
    }

    /* 初始化方法 */
    private void init() {
        id = current_index ++;
        userCode = UserCodeUtil.getUserCode();
    }

    public void setName(String name) {
        updateName(name);
        this.updated = new Date();
    }

    private void updateName(String name) {
        if (name != null && name.matches("^[\\u4e00-\\u9fa5]{2,4}$")) {
            this.name = name;
        }
    }

    public void setPhone(String phone) {
        updatePhone(phone);
        this.updated = new Date();
    }

    private void updatePhone(String phone) {
        if (phone != null && phone.matches("^1[3-9]\\d{9}$")) {
            this.phone = phone;
        }
    }

    public void setBirthday(LocalDate birthday) {
        updateBirthday(birthday);
        this.updated = new Date();
    }

    private void updateBirthday(LocalDate birthday) {
        if (birthday == null) {
            return;
        }
        if (LocalDate.now().isAfter(birthday)) {
            // 赋值生日
            this.birthday = birthday;
            // 计算年龄
            Period period = Period.between(birthday, LocalDate.now());
            this.age = period.getYears();
        }
    }

    public Integer getId() {
        return id;
    }

    public String getUserCode() {
        return userCode;
    }

    public String getName() {
        return name;
    }

    public String getPhone() {
        return phone;
    }

    public LocalDate getBirthday() {
        return birthday;
    }

    public Integer getAge() {
        return age;
    }

    public Date getCreated() {
        return created;
    }

    public String createdStr() {
        return DATE_FORMAT.format(created);
    }

    public Date getUpdated() {
        return updated;
    }

    public String updatedStr() {
        return updated == null ? "" : DATE_FORMAT.format(updated);
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", userCode='" + userCode + '\'' +
                ", name='" + name + '\'' +
                ", phone='" + phone + '\'' +
                ", birthday=" + birthday +
                ", age=" + age +
                ", created=" + createdStr() +
                ", updated=" + updatedStr() +
                '}';
    }

    public void nextBirth() {
        if (birthday == null) {
            System.out.println("生日尚未设置！");
            return;
        }
        // 先计算今年的生日过了没有
        LocalDate today = LocalDate.now();
        LocalDate nextBirth =
                birthday.withYear(today.getYear());
        // 比较today和nextBirth
        if (nextBirth.isBefore(today)) {
            nextBirth = nextBirth.withYear(today.getYear() + 1);
        }
        // 计算nextBirth和today的时间差
        Period period = Period.between(today, nextBirth);
        System.out.println("距离下次过生日还剩" + period.getMonths() +
                "月" + period.getDays() + "天;下次生日为" + nextBirth.getDayOfWeek() +
                "(星期" + nextBirth.getDayOfWeek().getValue() + ")");
    }

    /**
     * 充值
     * @param level 套餐级别
     */
    public void charge(int level) {
        if (vip == null) {
            vip = new Vip(level);
        } else {
            vip.renew(level);
        }
    }

    /**
     * 判断user是否为有效vip
     * @return 是否为有效vip
     */
    public boolean isVip() {
        if (vip == null) {
            return false;
        }
        Calendar calendar = Calendar.getInstance();
        // 不确定是否比过期时间早或晚，判断过期没有
        Date now = calendar.getTime();
        return now.before(vip.getEnd());
    }

    /**
     * 显示会员信息
     */
    public void showVip() {
        if (vip == null) {
            System.out.println(name + "还没开通过vip");
        } else {
            System.out.println(name + "vip信息如下：{" +
                    "开通时间:" + DATE_FORMAT.format(vip.getStart()) +
                    ", 到期时间:" + DATE_FORMAT.format(vip.getEnd()) +
                    "}");
        }
    }
}
